FROM centos:7

# This Dockerfile compiles WRF from source during "docker build" step
ENV WRF_VERSION 3.7.1

RUN yum -y update \
  && yum -y install file gcc gcc-gfortran gcc-c++ glibc.i686 libgcc.i686 libpng-devel jasper jasper-devel hostname m4 make perl \
  tar tcsh time wget which zlib zlib-devel openssh-clients openssh-server net-tools epel-release \
  && yum clean all
#
# now get 3rd party EPEL builds of netcdf and openmpi dependencies
RUN yum -y install netcdf-openmpi-devel.x86_64 netcdf-fortran-openmpi-devel.x86_64 netcdf-fortran-openmpi.x86_64 hdf5-openmpi.x86_64 openmpi.x86_64 openmpi-devel.x86_64 \
  && yum clean all
#
WORKDIR /root
USER root
#
# Download original sources
#
# RUN curl -SL http://www2.mmm.ucar.edu/wrf/src/WRFV$WRF_VERSION.TAR.gz | tar zxC /root \
# && curl -SL http://www2.mmm.ucar.edu/wrf/src/WPSV$WRF_VERSION.TAR.gz | tar zxC /root
COPY WRFV$WRF_VERSION.TAR.gz /root
RUN tar xvf WRFV$WRF_VERSION.TAR.gz -C /root
COPY WPSV$WRF_VERSION.TAR.gz /root
RUN tar xvf WPSV$WRF_VERSION.TAR.gz -C /root

#
# Set environment for interactive container shells
#
RUN echo export LDFLAGS="-lm" >> /etc/bashrc \
  && echo export NETCDF=/root/netcdf_links >> /etc/bashrc \
  && echo export JASPERINC=/usr/include/jasper/ >> /etc/bashrc \
  && echo export JASPERLIB=/usr/lib64/ >> /etc/bashrc \
  && echo export LD_LIBRARY_PATH="/usr/lib64/openmpi/lib" >> /etc/bashrc \
  && echo export PATH="/usr/lib64/openmpi/bin:$PATH" >> /etc/bashrc \
  && echo setenv LDFLAGS "-lm" >> /etc/csh.cshrc \
  && echo setenv NETCDF "/root/netcdf_links" >> /etc/csh.cshrc \
  && echo setenv JASPERINC "/usr/include/jasper/" >> /etc/csh.cshrc \
  && echo setenv JASPERLIB "/usr/lib64/" >> /etc/csh.cshrc \
  && echo setenv LD_LIBRARY_PATH "/usr/lib64/openmpi/lib" >> /etc/csh.cshrc \
  && echo setenv PATH "/usr/lib64/openmpi/bin:$PATH" >> /etc/csh.cshrc
#
# Build WRF first

# input 34 and 1 to configure script alternative line = && echo -e "34\r1\r" | ./configure
RUN mkdir netcdf_links \
  && ln -sf /usr/include/openmpi-x86_64/ netcdf_links/include \
  && ln -sf /usr/lib64/openmpi/lib netcdf_links/lib \
  && export NETCDF=/root/netcdf_links \
  && export JASPERINC=/usr/include/jasper/ \
  && export JASPERLIB=/usr/lib64/ \
  && cd ./WRFV3 \
  && ./configure <<< $'34\r1\r' \
  && sed -i -e '/^DM_CC/ s/$/ -DMPI2_SUPPORT/' ./configure.wrf \
  && /bin/csh ./compile em_real
#
# Build WPS second
#
# input 1 to configure script
RUN cd ./WPS \
  && export NETCDF=/root/netcdf_links \
  && export JASPERINC=/usr/include/jasper/ \
  && export JASPERLIB=/usr/lib64/ \
  && ./configure <<< $'1\r' \
  && sed -i -e 's/-L$(NETCDF)\/lib/-L$(NETCDF)\/lib -lnetcdff /' ./configure.wps \
  && /bin/csh ./compile
#
ENV LD_LIBRARY_PATH /usr/lib64/openmpi/lib
ENV PATH  /usr/lib64/openmpi/bin:$PATH
#
# copy in a couple custom scripts
COPY run-wrf /root
COPY docker-clean /root
RUN chmod +x /root/run-wrf \
  && chmod +x /root/docker-clean
#
# set up ssh configuration
COPY ssh_config /root/.ssh/config
RUN mkdir -p /root/.openmpi
COPY default-mca-params.conf /root/.openmpi/mca-params.conf
RUN /root/docker-clean \
    && mkdir -p /var/run/sshd \
    && ssh-keygen -A \
    && sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/g' /etc/ssh/sshd_config \
    && sed -i 's/#RSAAuthentication yes/RSAAuthentication yes/g' /etc/ssh/sshd_config \
    && sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config \
    && ssh-keygen -f /root/.ssh/id_rsa -t rsa -N '' \
    && chmod 600 /root/.ssh/config \
    && chmod 700 /root/.ssh \
    && cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
RUN rm /root/WRFV$WRF_VERSION.TAR.gz \
  && rm WPSV$WRF_VERSION.TAR.gz

# install oh-my-zsh
RUN yum install -y zsh git vim
# use GitHub
# && sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# use Gitee
# && sh -c "$(curl -fsSL https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh)" \
# && chmod +x /root/install.sh \
# && /root/install.sh
COPY install-ohmyzsh.sh /root
RUN chmod +x /root/install-ohmyzsh.sh \
  && /root/install-ohmyzsh.sh \
  && rm /root/install-ohmyzsh.sh \
  && rm /root/anaconda-ks.cfg
COPY zshrc /root/.zshrc